<?php
namespace Home\Model;
use Think\Model;
class WechatApiModel extends CommonModel {
	protected $autoCheckFields =false;
	
	public function getWebAccessToken($code){
		$json_res = httpResponseGET("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".C('WXMPAY_CONFIG.APPID')."&secret=".C('WXMPAY_CONFIG.APPSECRET')."&code=".$code."&grant_type=authorization_code");
		$http_res = json_decode($json_res,true);
		$data['access_token'] = $http_res['access_token'];
		$data['openid'] = $http_res['openid'];
		if(!empty($data['openid'])){
			$_SESSION['wechat_openid'] = $data['openid'];
			setcookie("wechat_openid",I('session.wechat_openid'),time()+3600*24*7,"/",C('COOKIE_DOMAIN'));
		}
		return $data;
	}
	
	public function getSnsapiUserinfo($web_access_token,$openid){
		$json_res = httpResponseGET("https://api.weixin.qq.com/sns/userinfo?access_token=".$web_access_token."&openid=".$openid."&lang=zh_CN");
		$http_res = json_decode($json_res,true);
		if(!empty($http_res)){
			$wechatUserInfo = D('WechatUserInfo');
			$user_id = I('session.user_id',null);
			$source_code = I('cookie.source_code',null);
			$db_res = $wechatUserInfo->where("openid = '".$http_res['openid']."'")->find();
			$data['openid'] = $http_res['openid'];
			$data['headimgurl'] = $http_res['headimgurl'];
			$data['nickname'] = $http_res['nickname'];
			$data['details'] = $json_res;
			if(!empty($user_id)) $data['user_id'] = $user_id;
			if(!empty($source_code)) $data['source_code'] = I('cookie.source_code',null);
			
			if(empty($db_res)){//add
				$wechatUserInfo->data($data)->add();
			}else{//save
				$wechatUserInfo->where("openid = '".$http_res['openid']."'")->save($data);
			}
		}
		return $http_res;
	}
	
	public function getCommonAccessToken($http=false){
		$access_token = "";
		$wechatAccessToken = D('WechatAccessToken');
		if($http){
			$json_res = httpResponseGET("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".C('WXMPAY_CONFIG.APPID')."&secret=".C('WXMPAY_CONFIG.APPSECRET'));
			$http_res = json_decode($json_res,true);
			if(!empty($http_res['access_token'])){
				$data['id'] = 1;
				$data['type'] = 0;
				$data['access_token'] = $http_res['access_token'];
				$data['update_time'] = date("Y-m-d H:i:s");
				$wechatAccessToken->where("id = 1")->save($data);
				$access_token = $http_res['access_token'];
			}
		}else{
			$update_time = date("Y-m-d H:i:s",time()-3600);
			$db_res = $this->query("select * from ar_shop.wechat_access_token where id=1 and type = 0 and update_time>'".$update_time."'");
			if(!empty($db_res[0]['access_token'])){
				$access_token = $db_res[0]['access_token'];
			}else{
				$access_token = $this->getCommonAccessToken(true);
			}
		}
		return $access_token;
	}
	
	function getJsapiTicket($http=false){
		$ticket = "";
		$wechatAccessToken = D('WechatAccessToken');
		if($http){
			$access_token = $this->getCommonAccessToken();
			$json_res = httpResponseGET("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");
			$http_res = json_decode($json_res,true);
			if(!empty($http_res['ticket'])){
				$data['id'] = 2;
				$data['type'] = 1;
				$data['access_token'] = $http_res['ticket'];
				$data['update_time'] = date("Y-m-d H:i:s");
				$wechatAccessToken->where("id = 2")->save($data);
				$ticket = $http_res['ticket'];
			}
		}else{
			$update_time = date("Y-m-d H:i:s",time()-3600);
			$db_res = $this->query("select * from ar_shop.wechat_access_token where id = 2 and type = 1 and update_time>'".$update_time."'");
			if(!empty($db_res[0]['access_token'])){
				$ticket = $db_res[0]['access_token'];
			}else{
				$ticket = $this->getJsapiTicket(true);
			}
		}
		return $ticket;
	}
	
	function getJsapiSignature($jsapi_ticket,$url){
		$jsapi['noncestr'] = "shishifresh";
		$jsapi['jsapi_ticket'] = $jsapi_ticket;
		$jsapi['timestamp'] = time();
		$jsapi['url'] = $url;
		ksort($jsapi);
		$string1 = "";
		foreach($jsapi as $k=>$v){
			if($string1 == ""){
				$string1 = $k."=".$v;
			}else{
				$string1 = $string1."&".$k."=".$v;
			}
		}
		return array('signature'=>sha1($string1),'nonceStr'=>$jsapi['noncestr'],'timestamp'=>$jsapi['timestamp']);
	}
	
	function grouponSendMsg($openid,$url,$title,$commodityname,$nickname,$remark){
		$access_token = $this->getCommonAccessToken();
		$para['touser'] = $openid;
		$para['template_id'] = C('WEXIN_MSG_TEMPLATE.GROUPON');
		$para['url'] = $url;
		$para['data']['first']['value'] = $title;//'恭喜您，您已经组团成功，可以购买商品了！';
		$para['data']['Pingou_ProductName']['value'] = $commodityname;//'荔枝团';
		$para['data']['Weixin_ID']['value'] = $nickname;//'朱鹏飞';
		$para['data']['Remark']['value'] = $remark;
		
		$para['topcolor'] = '#FF0000';
		$para['data']['Weixin_ID']['color'] = '#173177';
		$para['data']['Pingou_ProductName']['color'] = '#173177';
		$para['data']['first']['color'] = '#173177';
		$para['data']['Remark']['color'] = '#173177';;
		// dump($access_token);
		// dump($para);exit;
		$para = yzg_json_encode($para);
		$res = httpResponsePOST("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token, $para);
		
		// sys_debug_log("**********************grouponSendMsg*****************");
		// sys_debug_log($res);
		return $res;
	}
	
}
?>